package com.android.publicModule.webview;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;

import com.android.publicModule.network.DownloadProgress;
import com.android.publicModule.tools.Tools;
import com.android.publicModule.webview.download.DownloadManage;
import com.android.publicModule.webview.handler.HandlerManage;
import com.android.publicModule.webview.sdk.Api;

public class WebViewControl {
	
	private static WebViewControl mWebViewControl;
	private Context mContext;
	private DownloadManage mDownloadManage;
	private HandlerManage mHandlerManage;
	
	public WebViewControl(Context context) {
		if(context!=null)
		{
			mContext = context.getApplicationContext();
			mDownloadManage = new DownloadManage(context);
			mHandlerManage = new HandlerManage(context);
		}
	}
	
	public static WebViewControl getInstance(Context context) {
		if(mWebViewControl==null)
		{
			mWebViewControl = new WebViewControl(context);
		}
		return mWebViewControl;
	}
	
	public void downloadHtml(String id,String url,String savePath,DownloadProgress progress,Object downloadInfo) {
		if(mDownloadManage!=null)
		{
			mDownloadManage.downloadHtml(id, url, savePath, progress, downloadInfo);
		}
	}
	
	public void downloadImg(String id,String url,String savePath,DownloadProgress progress,Object downloadInfo) {
		if(mDownloadManage!=null)
		{
			mDownloadManage.downloadImg(id, url, savePath, progress, downloadInfo);
		}
	}
	
	public void downloadApk(String id,String url,String savePath,DownloadProgress progress,Object downloadInfo) {
		if(mDownloadManage!=null)
		{
			mDownloadManage.downloadApk(id, url, savePath, progress, downloadInfo);
		}
	}
	
	public void downloadFile(String id,String url,String savePath,DownloadProgress progress,Object downloadInfo) {
		if(mDownloadManage!=null)
		{
			mDownloadManage.downloadFile(id, url, savePath, progress, downloadInfo);
		}
	}
	
	/**
	 * 显示TOAST文本提示信息
	 * @param text
	 */
	public void showToast(String text,int time) {
		if(mHandlerManage!=null)
		{
			mHandlerManage.showToast(text, time);
		}
	}
	
	public String callJsonFunctionString(JSONObject jsonFun,JSONObject jsonExt) {
		if(jsonFun!=null)
		{
			int functionId = jsonFun.optInt("functionId",-1);
			if(functionId<0)
			{
				String functionName = jsonFun.optString("functionName");
				functionId = Api.getFunctionId(functionName);
			}
			if(functionId>=0)
			{
				switch(functionId) {
				
					case Api.getFunctionId_API_FUNCTION_ID:
						String forFunctionName = jsonFun.optString("forFunctionName");
						return String.valueOf(Api.getFunctionId(forFunctionName));
						
					case Api.getApiVersionCode_API_FUNCTION_ID:
						return String.valueOf(Api.getApiVersionCode());
						
					default :
						break;
						
				}
			}
		}
		return null;
	}
    
    public String doMyActionString(String jsonInfo) {
    	String result = null;
    	if(jsonInfo!=null&&jsonInfo.length()>0)
    	{
    		JSONObject jsonRoot = Tools.parserJSONObject(jsonInfo);
    		if(jsonRoot!=null)
    		{
    			JSONObject jsonExt = jsonRoot.optJSONObject("jsonExt");
    			JSONObject jsonStart = jsonRoot.optJSONObject("jsonStart");
    			if(jsonStart!=null)
    			{
    				result = callJsonFunctionString(jsonStart,jsonExt);
    			}
    			
    			JSONArray jsonArr = jsonRoot.optJSONArray("jsonArr");
    			
    			if(jsonArr!=null&&jsonArr.length()>0)
    			{
    				for(int i=0;i<jsonArr.length();i++)
    				{
    					callJsonFunctionString(jsonArr.optJSONObject(i),jsonExt);
    				}
    			}
    			
    			JSONObject jsonEnd = jsonRoot.optJSONObject("jsonEnd");
    			if(jsonEnd!=null)
    			{
    				result = callJsonFunctionString(jsonEnd,jsonExt);
    			}
    		}
    	}
    	return result;
    }
	
}
